package logic

import (
	"context"

	"share/internal/svc"
	"share/pb/share"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetInfoByCodeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetInfoByCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetInfoByCodeLogic {
	return &GetInfoByCodeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetInfoByCode 根据code获取详情
func (l *GetInfoByCodeLogic) GetInfoByCode(in *share.CodeRequest) (*share.ShareInfo, error) {
	s, err := l.svcCtx.ShareAggregationModel.GetShareByCode(l.ctx, in.ShareLink, in.ShareCode)
	if err != nil {
		return nil, err
	}

	resp := &share.ShareInfo{
		Id:         s.Id,
		UserId:     s.UserId,
		ShareLink:  s.ShareLink,
		ShareCode:  s.ShareCode,
		ShareType:  s.ShareType,
		Permission: s.Permission,
		ExpireType: s.ExpireType,
		ExpireTime: 0,
		Status:     s.Status,
		FolderIds:  s.FolderIDs,
		FileIds:    s.FileIDs,
		CreateAt:   s.CreateAt.Unix(),
		UpdateAt:   s.UpdateAt.Unix(),
	}
	if s.ExpireTime.Valid {
		val, _ := s.ExpireTime.Value()
		resp.ExpireTime = val.(int64)
	}
	return resp, nil
}
